package com.duan.interceptor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.duan.config.JsonParameterRequestWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Parameter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * @author db
 * @version 1.0
 * @description LogDemoInterceptor
 * @since 2024/1/3
 */
@Component
@Slf4j
public class LogDemoInterceptor implements HandlerInterceptor {
    // 目标方法执行前调用  true：放行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        System.out.println("preHandle");


        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String requestTime = sdf.format(new Date());
        request.setAttribute("requestTime",requestTime);
        System.out.println("请求时间"+requestTime);

        JsonParameterRequestWrapper jsonParameterRequestWrapper = new JsonParameterRequestWrapper(request);
        String bodyMessage = jsonParameterRequestWrapper.getBodyMessage();

        request.setAttribute("requestParams",bodyMessage);


        String requestMethod = request.getMethod();
        request.setAttribute("requestMethod",requestMethod);
        return true;
    }

    // 目标方法执行后调用
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle...");

    }


    // 请求处理后调用
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        JSONObject json = new JSONObject();
        // 接口请求时间
        String requestTime = (String)request.getAttribute("requestTime");
        json.put("requestTime",requestTime);

        // 请求参数
        Object requestParams = request.getAttribute("requestParams");
        json.put("requestParams",requestParams.toString());

        // 请求方法
        Object requestMethod = request.getAttribute("requestMethod");
        json.put("requestMethod",requestMethod.toString());

        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Date parse = null;
        try {
            parse = sdf.parse(requestTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long time = parse.getTime();

        // 接口响应时间
        long responseTime = System.currentTimeMillis();
        Date date = new Date(responseTime);
        String format = sdf.format(date);
        json.put("format",format);

        // 接口耗时
        long consumeTime =  responseTime - time;
        json.put("consumeTime",consumeTime);

        Object responseParams = request.getAttribute("responseParams");
        json.put("responseParams",responseParams);

        log.info(String.valueOf(json));
        System.out.println("Completion...");
    }
}
